package com.example.pceb.service.Impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pceb.entity.Enterprise;
import com.example.pceb.entity.Recruit;
import com.example.pceb.mapper.EnterpriseMapper;
import com.example.pceb.mapper.RecruitMapper;
import com.example.pceb.result.Result;
import com.example.pceb.result.ResultCode;
import com.example.pceb.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

//企业端
//实现招聘信息的增删改查
@Service
public class EnterPriseServiceImpl extends ServiceImpl<EnterpriseMapper,Enterprise> implements EnterpriseService {
    @Autowired
    private RecruitMapper recruitMapper;
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    //根据用户名获取企业信息
    @Override
    public Enterprise getByUsername(String username) {
        QueryWrapper<Enterprise> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return enterpriseMapper.selectOne(wrapper);
    }
    //获取公司所有招聘信息
    @Override
    public Result<List<Recruit>> getRecruitsByEnterprise(String enterprise) {
        QueryWrapper<Recruit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise", enterprise);
        List<Recruit> recruits = recruitMapper.selectList(queryWrapper);
        return Result.success(recruits);
    }

    //根据id删除某个招聘岗位
    @Override
    public Result<String> deleteRecruit(int recruitId) {
        boolean success = recruitMapper.deleteById(recruitId)>0;
        if (success){
            return Result.success("删除成功！");
        }else {
            return Result.fail("删除失败！", ResultCode.OK);
        }
    }

    //公司编辑招聘岗位详细信息
    @Override
    public Result<String> editRecruit(Recruit recruit) {
        System.out.println(recruit.getUsername());
        boolean success = recruitMapper.updateById(recruit)>0;
        if (success){
            return Result.success("编辑成功！");
        }else {
            return Result.fail("编辑失败！", ResultCode.OK);
        }
    }

    //公司新增招聘信息
    @Override
    public Result<String> addRecruit(Recruit recruit) {
        boolean success = recruitMapper.insert(recruit)>0;
        if (success){
            return Result.success("新增成功！");
        }else {
            return Result.fail("新增失败！", ResultCode.OK);
        }
    }

    //获取所有公司位置
    public List<Map<String, String>> getAllEnterprisesForMap() {
        return enterpriseMapper.selectAllEnterprisesForMap();
    }


//    //获取企业经纬度
//    private static final String AMAP_KEY = "b151b927185bef5e16bbb6e390a76c31";
//    private RestTemplate restTemplate = new RestTemplate();
//
//
//    public void batchGeocode() throws InterruptedException {
//        // 查询所有未地理编码的企业
//        QueryWrapper<Enterprise> wrapper = new QueryWrapper<>();
//        wrapper.isNull("lng").or().isNull("lat");
//        List<Enterprise> list = enterpriseMapper.selectList(wrapper);
//
//        for (Enterprise e : list) {
//            double[] lnglat = geocode(e.getAddress());
//            if (lnglat != null) {
//                e.setLng(lnglat[0]);
//                e.setLat(lnglat[1]);
//                enterpriseMapper.updateById(e);
//            }
//            Thread.sleep(200); // 防止QPS超限
//        }
//    }
//
//    private double[] geocode(String address) {
//        try {
//            String url = "https://restapi.amap.com/v3/geocode/geo?address=" +
//                    URLEncoder.encode(address, "UTF-8") + "&key=" + AMAP_KEY;
//            String resp = restTemplate.getForObject(url, String.class);
//            JSONObject json = JSONObject.parseObject(resp);
//            if ("1".equals(json.getString("status"))) {
//                JSONArray geocodes = json.getJSONArray("geocodes");
//                if (geocodes != null && !geocodes.isEmpty()) {
//                    String location = geocodes.getJSONObject(0).getString("location");
//                    String[] arr = location.split(",");
//                    return new double[]{Double.parseDouble(arr[0]), Double.parseDouble(arr[1])};
//                }
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//        return null;
//    }

    @Override
    public List<String> getAllEnterpriseIds() {
        // 查询所有企业ID
        return enterpriseMapper.selectAllIds();
    }

    @Override
    public Enterprise getById(String id) {
        return enterpriseMapper.selectById(id);
    }

    @Override
    public int countAllEnterprises() {
        return enterpriseMapper.countAllEnterprises();
    }
}
